package com.zyx.xunyilu.utils;

import com.zyx.xunyilu.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @ClassName: EmailUtils
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2019/7/27 12:19
 */
@Slf4j
@Component
public class EmailUtils {

    @Value("${spring.mail.username}")
    private String form;

    @Autowired
    private JavaMailSender sender;

    /**
     * 发送注册确定邮件
     * @param to 接收方
     * @param title 邮件标题
     * @param html 邮件内容
     */
    @Async
    public void send(String to, String title, String html) {
        MimeMessage mimeMessage = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(form);
            helper.setTo(to);
            helper.setSubject(title);
            helper.setText(html, true);
            sender.send(mimeMessage);
        } catch (MessagingException e) {
            log.error("error {}", e);
            log.error("邮件发送失败 {}", e.getMessage());
        }
    }

    @Async
    public void regEmail(String to, User user, String url) {
        String name = user.getName();
        String html = regTemplate.replace("${name}", name).replace("${link}", url);
        send(to, "寻医路注册邮件", html);
    }

    @Async
    public void backPwdEmail(User user, String code) {
        String html = codeTemplate.replace("${name}", user.getName()).replace("${code}", code);
        send(user.getEmail(), "寻医路验证码", html);
    }

    @Async
    public void changeEmail(User user, String email, String code) {
        String html = emailCodeTemplate.replace("${name}", user.getName()).replace("${code}", code);
        send(email, "寻医路验证码", html);
    }

    private final String emailCodeTemplate = "<!DOCTYPE html>\n" +
            "<html lang=\"en\">\n" +
            "<head>\n" +
            "  <meta charset=\"UTF-8\">\n" +
            "  <meta name=\"viewport\"\n" +
            "        content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\">\n" +
            "  <title>寻医路 验证码邮件</title>\n" +
            "  <style>\n" +
            "    @media screen and (min-width: 800px) {\n" +
            "      .box {\n" +
            "        width: 360px;\n" +
            "      }\n" +
            "    }\n" +
            "  </style>\n" +
            "</head>\n" +
            "<body style=\"margin: 0;\">\n" +
            "<div class=\"box\" style=\"margin: 0 auto; border: 1px solid #3d484c;\">\n" +
            "  <nav style=\"background-color: #3d484c;height: 45px;\">\n" +
            "    <div style=\"float: right;color: #ffffff;padding: 12px\">寻医路</div>\n" +
            "  </nav>\n" +
            "  <div style=\"padding: 0 8px;\">\n" +
            "    <h3 style=\"margin: 15px 2px;\">\n" +
            "      尊敬的 <span style=\"color: #31bdeb;\">${name}</span>\n" +
            "    </h3>\n" +
            "    <p style=\"margin: 5px 0; font-size: 14px;\">您当前正在进行更换邮箱操作，本次操作的验证码如下：</p>\n" +
            "    <p style=\"margin: 15px 2px;\">\n" +
            "    <h3>${code}</h3>\n" +
            "    </p>\n" +
            "    <div style=\"font-size: 12px; color: #707070;\">\n" +
            "      <p style=\"margin: 5px 0;\">本验证码3分钟内有效。如非本人操作，请忽略本邮件。</p>\n" +
            "    </div>\n" +
            "    <h4 style=\"margin: 15px 2px;\">创客实验室 团队</h4>\n" +
            "  </div>\n" +
            "  <footer style=\"height: 5px;background-color: #3d484c;\"></footer>\n" +
            "</div>\n" +
            "</body>\n" +
            "</html>";

    private final String codeTemplate = "<!DOCTYPE html>\n" +
            "<html lang=\"en\">\n" +
            "<head>\n" +
            "  <meta charset=\"UTF-8\">\n" +
            "  <meta name=\"viewport\"\n" +
            "        content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\">\n" +
            "  <title>寻医路 验证码邮件</title>\n" +
            "  <style>\n" +
            "    @media screen and (min-width: 800px) {\n" +
            "      .box {\n" +
            "        width: 360px;\n" +
            "      }\n" +
            "    }\n" +
            "  </style>\n" +
            "</head>\n" +
            "<body style=\"margin: 0;\">\n" +
            "<div class=\"box\" style=\"margin: 0 auto; border: 1px solid #3d484c;\">\n" +
            "  <nav style=\"background-color: #3d484c;height: 45px;\">\n" +
            "    <div style=\"float: right;color: #ffffff;padding: 12px\">寻医路</div>\n" +
            "  </nav>\n" +
            "  <div style=\"padding: 0 8px;\">\n" +
            "    <h3 style=\"margin: 15px 2px;\">\n" +
            "      尊敬的 <span style=\"color: #31bdeb;\">${name}</span>\n" +
            "    </h3>\n" +
            "    <p style=\"margin: 5px 0; font-size: 14px;\">您当前正在进行找回密码操作，本次操作的验证码如下：</p>\n" +
            "    <p style=\"margin: 15px 2px;\">\n" +
            "    <h3>${code}</h3>\n" +
            "    </p>\n" +
            "    <div style=\"font-size: 12px; color: #707070;\">\n" +
            "      <p style=\"margin: 5px 0;\">本验证码3分钟内有效。如非本人操作，请忽略本邮件。</p>\n" +
            "    </div>\n" +
            "    <h4 style=\"margin: 15px 2px;\">创客实验室 团队</h4>\n" +
            "  </div>\n" +
            "  <footer style=\"height: 5px;background-color: #3d484c;\"></footer>\n" +
            "</div>\n" +
            "</body>\n" +
            "</html>";

    private final String regTemplate = "<!DOCTYPE html>\n" +
            "<html lang=\"en\">\n" +
            "<head>\n" +
            "  <meta charset=\"UTF-8\">\n" +
            "  <meta name=\"viewport\"\n" +
            "        content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\">\n" +
            "  <title>寻医路 确定邮件</title>\n" +
            "  <style>\n" +
            "    @media screen and (min-width: 800px) {\n" +
            "      .box {\n" +
            "        width: 360px;\n" +
            "      }\n" +
            "    }\n" +
            "  </style>\n" +
            "</head>\n" +
            "<body style=\"margin: 0;\">\n" +
            "<div class=\"box\" style=\"margin: 0 auto; border: 1px solid #3d484c;\">\n" +
            "  <nav style=\"background-color: #3d484c;height: 45px;\">\n" +
            "    <div style=\"float: right;color: #ffffff;padding: 12px\">寻医路</div>\n" +
            "  </nav>\n" +
            "  <div style=\"padding: 0 8px;\">\n" +
            "    <h3 style=\"margin: 15px 2px;\">\n" +
            "      尊敬的 <span style=\"color: #31bdeb;\">${name}</span>\n" +
            "    </h3>\n" +
            "    <p style=\"margin: 5px 0; font-size: 13px;\">欢迎加入寻医路，您离成功注册仅有一步之遥。请点击以下链接完成您的注册。</p>\n" +
            "    <p style=\"margin: 15px 2px;\">\n" +
            "      <a href=\"${link}\" style=\"color: #31bdeb;\">${link}</a>\n" +
            "    </p>\n" +
            "    <div style=\"font-size: 12px; color: #707070;\">\n" +
            "      <p style=\"margin: 5px 0;\">如果以上链接无法访问，请将该网址复制并粘贴至新的浏览器窗口中，该链接仅限本次操作。</p>\n" +
            "      <p style=\"margin: 5px 0;\">如果并非本人注册，您无需执行任何操作来取消账号！此账号将不会启动。</p>\n" +
            "      <p style=\"margin: 5px 0;\">本链接将在5分钟后失效！</p>\n" +
            "      <p style=\"font-size: 10px; text-align: right;\">此邮件为系统邮件，请勿直接回复。</p>\n" +
            "    </div>\n" +
            "    <h4 style=\"margin: 15px 2px;\">寻医路 团队</h4>\n" +
            "  </div>\n" +
            "  <footer style=\"height: 5px;background-color: #3d484c;\"></footer>\n" +
            "</div>\n" +
            "</body>\n" +
            "</html>";
}
